package com.rt.code.generator.freemarker.exec;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rt.code.bean.A1templatecolumn;
import com.rt.code.bean.A1templatetable;
import com.rt.code.generator.freemarker.dto.GenerateDto;
import com.rt.code.mapper.A1templatecolumnMapper;
import com.rt.code.mapper.A1templatetableMapper;
import com.rt.easyjava.common.utils.others.SwitchLineCamel;
import com.rt.easyjava.common.utils.zip.ZipUtils;
import com.rt.tmpt.dto.A1TemplateColumnDto;
import com.rt.tmpt.dto.A1TemplateTableDto;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.utils.JsonMap;
import com.rt.tmpt.utils.StringUtils;
import com.rt.tmpt.service.ITemplateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.zip.ZipOutputStream;

/**
 * Created by wxy on 2017/1/28.
 */
@Scope("prototype")
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class FreemarkerGen {
    @Resource private ITemplateService templateService;
    @Resource private A1templatetableMapper a1templatetableMapper;
    @Resource private A1templatecolumnMapper a1templatecolumnMapper;

    private A1TemplateTableDto templateTable;
    private String prjPath = "/zpmng/";
    private FmPageUtils fmPageUtils ;
    private FmUtils fmUtils ;
    public String _keyRandom = "";
    private String datasource = "dsA";



    public FmPageUtils initTemplateTable(String _keyRandom){
        this._keyRandom = _keyRandom;
        this.templateTable = templateService.getTemplateTable(_keyRandom);
        this.datasource = templateTable.getDataSource();
        if(StringUtils.isNull(datasource)){this.datasource = "dsA";}
        this.fmPageUtils = new FmPageUtils(templateService,templateTable,_keyRandom);
        this.fmUtils = new FmUtils(this.datasource);
        return fmPageUtils;
    }


    /**
     *
     * @throws Exception
     */
    public Map<String, Object> getMap(String module){
        String stripPrefix = "";
        //取数据库列类型和table的alias（Map<String,String>）
        Map<String,String[]> paramMap = new HashMap<String, String[]>();
        paramMap.put("_keyRandom",new String[]{_keyRandom});
        paramMap.put("pageCurrent",new String[]{"1"});
        paramMap.put("pageSize",new String[]{"10"});
        paramMap.put("sortOrder",new String[]{"desc"});
        paramMap.put("sortColumn",new String[]{"id"});
        //map to jsonstring
        String paramJson = JsonMap.map2Json(paramMap);
        System.out.println("json:" + paramJson);
        Result resultObj = null;
        try {
            resultObj = templateService.genSql4DynamicDS(paramJson);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Map<String,Map<String,String>> dbcolumnsProperty = (Map<String,Map<String,String>>)resultObj.getMap().get("dbcolumnsProperty");
        Map<String,String> columnAliasMap = (Map<String,String>)resultObj.getMap().get("columnAliasMap");
        Map<String,String> columnTableMap = (Map<String,String>)resultObj.getMap().get("columnTableMap");

        //主键和数据类型
        String primaryKey = templateTable.getPrimaryKey();
        String primaryKeyDbType = dbcolumnsProperty.get(templateTable.getTableName()+"_"+primaryKey.toLowerCase()).get("data_type");
        String primaryKeyDbTypeLower = primaryKeyDbType.toLowerCase();
        String primaryKeyDataType4JavaEncap = "";
        if(primaryKeyDbTypeLower.equals("int")){
            primaryKeyDataType4JavaEncap = "Integer";
        }else if(primaryKeyDbTypeLower.equals("long") || primaryKeyDbTypeLower.equals("bigint")){
            primaryKeyDataType4JavaEncap = "Long";
        }

        //选择optionId时，filedList是要隐藏的域
        String hiddenFiledsJson = fmUtils.dealHiddenFiledsJson(templateTable);

        //生成类名称
        String tableName = templateTable.getTableName();
        String tableComment = templateTable.getComment();

        String tableNameStripPrefix = tableName.replaceFirst(stripPrefix,"");
        String className = FmUtils.switch2Property(tableNameStripPrefix);
        //第一个字母大写
        String tableNameFirstWordUpper = FmUtils.firstWord2Upper(tableName);
        String tableNameFirstWordLower = FmUtils.firstWord2Lower(tableName);
        String classNameFirstWordUpper = FmUtils.firstWord2Upper(className);
        String classNameFirstWordLower = FmUtils.firstWord2Lower(className);

        Map<String, String> myParamMap = new HashMap<>();
        myParamMap.put("module", module);
        myParamMap.put("classInstance", classNameFirstWordLower);


        // 取多对一查询时的所有表名
        List<String> tableNameList = new ArrayList<>();
        List<String> tableAliasList = new ArrayList<>();
        FmUtils.parseTableNameSql(templateTable.getTableNameSql(), tableNameList, tableAliasList);
        boolean hasM2O = false;
        String tableNameM2O = "";
        String classNameM2O = "";
        String tableNameFirstWordUpperM2O = "";
        String tableNameFirstWordLowerM2O = "";
        String classNameFirstWordUpperM2O = "";
        String classNameFirstWordLowerM2O = "";
        List<Map<String,String>> tableNameM2OList = new ArrayList<>();
        if(tableNameList.size() >= 2) {
            // >=2张表，第1张表名是主表，其后是从表
            hasM2O = true;
            tableNameM2O = tableNameList.get(1);
            String tableNameM2OStripPrefix = tableNameM2O.replaceFirst(stripPrefix,"");
            classNameM2O = FmUtils.switch2Property(tableNameM2OStripPrefix);
            tableNameFirstWordUpperM2O = FmUtils.firstWord2Upper(tableNameM2O);
            tableNameFirstWordLowerM2O = FmUtils.firstWord2Lower(tableNameM2O);
            classNameFirstWordUpperM2O = FmUtils.firstWord2Upper(classNameM2O);
            classNameFirstWordLowerM2O = FmUtils.firstWord2Lower(classNameM2O);

            for (int j=1; j<tableNameList.size(); j++) {
                String _tableNameM2O = tableNameList.get(j);
                String _tableNameM2OStripPrefix = _tableNameM2O.replaceFirst(stripPrefix,"");
                String _classNameM2O = FmUtils.switch2Property(_tableNameM2OStripPrefix);
                String _classNameFirstWordUpperM2O = FmUtils.firstWord2Upper(_classNameM2O);

                Map<String, String> map = new HashMap<>();
                map.put("tableName", _tableNameM2O);
                map.put("classInstance", _classNameM2O);
                map.put("className", _classNameFirstWordUpperM2O);
                map.put("alias", "t"+(j+1)); // t2,t3,t4

                tableNameM2OList.add(map);

            }
        }

        // Map: tableAlias <--> tableName
        // 取多对一查询时的所有表名
        Map<String, String> mapTableAliasToName = FmUtils.mapTableAliasToName(tableAliasList, tableNameList);

        //把数据库列的一些属性封装进来
        List<Map<String,Object>> columnList = new ArrayList<Map<String, Object>>();
        //can add/edit column
        List<Map<String,Object>> columnCanAddList = new ArrayList<Map<String, Object>>();
        //need sum column list
        List<Map<String,Object>> columnNeedSumList  = new ArrayList<Map<String, Object>>();
        // ds = 2 , sql取值
        List<Map<String,Object>> columnSqlOptionList  = new ArrayList<Map<String, Object>>();
        //query: mapper.xml中查询条件列 where子句
        List<Map<String,Object>> conditionColumnMapList = new ArrayList<Map<String, Object>>();
        //列多选项情况
        List<A1TemplateColumnDto> templateTableColumnOptionList = templateService.getTemplateTableColumnOptionList(_keyRandom);

        //整理‘列’和‘可编辑列’
        Map<String, Object> resultMap = fmUtils.dealColumnList(templateTable, dbcolumnsProperty, columnList, columnCanAddList, columnNeedSumList,
                conditionColumnMapList, columnAliasMap,columnTableMap,templateTableColumnOptionList,
                columnSqlOptionList, mapTableAliasToName, myParamMap);
        boolean needSum = columnNeedSumList.size()>0?true:false;
        // 是否是树表
        // Boolean isTreeTable = (Boolean)resultMap.get("isTreeTable"); ``` ```
        //整理可编辑列成字符串，mapper.xml中insert和update相关列使用字符串
        //can add/edit column
        StringBuffer sbCanAddColumn = new StringBuffer();
        StringBuffer sbCanAddColumnValue = new StringBuffer();
        StringBuffer sbCanEditColumn = new StringBuffer();
        fmUtils.dealCanEditColumn(columnCanAddList,sbCanAddColumn,sbCanAddColumnValue,sbCanEditColumn);
        String canAddColumnStr = sbCanAddColumn.toString();
        String canAddColumnValueStr = sbCanAddColumnValue.toString();
        String CanEditColumnStr = sbCanEditColumn.toString();

        //查询字段列 eg:select c1,c2,c3  mapper.xml中使用
        StringBuffer sb4columnStr = new StringBuffer();
        fmUtils.dealColumnstr4Select(templateTable,columnList,sb4columnStr);
        String columnStr = sb4columnStr.toString();

        //字段的sum string eg: select sum(balance) from table1 mapper.xml中使用
        StringBuffer sb4columnSumStr = new StringBuffer();
        /*List<Map<String,String>> sumTitleList = new ArrayList<Map<String, String>>();*/
        Map<String,String> sumTitleMap = new HashMap<String, String>();
        fmUtils.dealNeedSumColumn(columnNeedSumList, sb4columnSumStr,sumTitleMap);
        String columnSumStr = sb4columnSumStr.toString();

        //pages
        Map<String, Object> columnsTag = fmPageUtils.getColumnList(dbcolumnsProperty,columnTableMap);
        String templateTableColumnOptionJson = fmPageUtils.getTemplateTableColumnOptionJson();

        // config json
        String cfgJsonStr = templateTable.getTreeJson();
        Map<String, Object> configMap = FmUtils.parseConfigJson(cfgJsonStr, tableName);
        JSONObject cfgJson = (JSONObject)configMap.get("cfgJson");
        // tree
        Boolean isTreeTable = (Boolean)configMap.get("isTreeTable");
        // One to Many
        List<Map> o2mList = (List<Map>)configMap.get("o2mList");
        /*Boolean hasO2M = (Boolean)configMap.get("hasO2M");
        String tableNameO2M = (String)configMap.get("tableNameO2M");
        String classNameO2M = (String)configMap.get("classNameO2M");
        String tableNameFirstWordUpperO2M = (String)configMap.get("tableNameFirstWordUpperO2M");
        String tableNameFirstWordLowerO2M = (String)configMap.get("tableNameFirstWordLowerO2M");
        String classNameFirstWordUpperO2M = (String)configMap.get("classNameFirstWordUpperO2M");
        String classNameFirstWordLowerO2M = (String)configMap.get("classNameFirstWordLowerO2M");*/
        // hasAsideTree
        Boolean hasAsideTree = (Boolean)configMap.get("hasAsideTree");

        //freemarker 参数
        Map<String, Object> map = new HashMap<>();
        map.put("isTreeTable", isTreeTable);
        map.put("hasAsideTree", hasAsideTree);
        //
        map.put("tableNameFirstWordUpper", tableNameFirstWordUpper);
        map.put("tableNameFirstWordLower", tableNameFirstWordLower);
        map.put("classNameFirstWordUpper", classNameFirstWordUpper);
        map.put("classNameFirstWordLower", classNameFirstWordLower);
        // many to one : has association table
        map.put("hasM2O", hasM2O);
        map.put("tableNameM2OList", tableNameM2OList);
        map.put("tableNameM2O", tableNameM2O);
        map.put("classNameM2O", classNameM2O);
        map.put("tableNameFirstWordUpperM2O", tableNameFirstWordUpperM2O);
        map.put("tableNameFirstWordLowerM2O", tableNameFirstWordLowerM2O);
        map.put("classNameFirstWordUpperM2O", classNameFirstWordUpperM2O);
        map.put("classNameFirstWordLowerM2O", classNameFirstWordLowerM2O);
        // one to many
        map.put("o2mList", o2mList);
        /*map.put("hasO2M", hasO2M);
        map.put("tableNameFirstWordUpperO2M", tableNameFirstWordUpperO2M);
        map.put("tableNameFirstWordLowerO2M", tableNameFirstWordLowerO2M);
        map.put("classNameFirstWordUpperO2M", classNameFirstWordUpperO2M);
        map.put("classNameFirstWordLowerO2M", classNameFirstWordLowerO2M);*/
        // end
        map.put("columnList", columnList);
        map.put("columnStr", columnStr);//mapper.xml Base_Column_List
        map.put("tableNameSql", StringUtils.isNull(templateTable.getTableNameSql())?templateTable.getTableName():templateTable.getTableNameSql());
        map.put("templateTableId",templateTable.getId());
        map.put("idType",templateTable.getIdType());
        map.put("btnMulti",templateTable.getBtnMulti());
        map.put("tableName",templateTable.getTableName());
        map.put("tableComment",templateTable.getComment());
        map.put("className",className);
        map.put("primaryKey", primaryKey);
        map.put("foreignKey", templateTable.getForeignKey());
        map.put("foreignKey_PropertyFormat", FmUtils.switch2Property(templateTable.getForeignKey()));
        map.put("primaryKey_PropertyFormat", FmUtils.switch2Property(primaryKey));
        map.put("primaryKeyDbType", primaryKeyDbType);
        map.put("primaryKeyDbTypeLower", primaryKeyDbTypeLower);
        map.put("primaryKeyDataType4JavaEncap", primaryKeyDataType4JavaEncap);
        map.put("primaryKeyFirstWordUpper", FmUtils.firstWord2Upper(templateTable.getPrimaryKey()));
        map.put("conditionColumnMapList", conditionColumnMapList);
        map.put("tableAliasName", templateTable.getTableAliasName());
        //can add/edit column
        map.put("columnCanAddList", columnCanAddList);
        map.put("canAddColumnStr", canAddColumnStr);
        map.put("canAddColumnValueStr", canAddColumnValueStr);
        map.put("CanEditColumnStr", CanEditColumnStr);
        //enum
        // map.put("optionDataList", optionDataList);
        // map.put("option4ColumnSelfList", option4ColumnSelfList);
        //need sum column str
        map.put("needSum", needSum);
        map.put("columnSumStr", columnSumStr);
        map.put("sumTitleMap", sumTitleMap);
        // ds = 2 , sql取值
        map.put("columnSqlOptionList", columnSqlOptionList);
        // tree json
        map.put("cfgJson", cfgJson);
        map.put("cfgJsonStr", cfgJsonStr);

        //pages
        map.put("path", prjPath);
        map.put("_keyRandom", _keyRandom);
        map.put("columnsTag", columnsTag);
        map.put("templateTable", templateTable);
        map.put("templateTableColumnOptionJson", templateTableColumnOptionJson);
        map.put("templateTableColumnOptionList", fmPageUtils.getTemplateTableColumnOptionList());
        map.put("sortColumnList", fmPageUtils.getSortColumnList());
        map.put("hiddenFiledsJson", hiddenFiledsJson);

        return map;
    }

    /**
     *
     * @throws Exception
     */
    public Map<String, Object> getEnumMap(){
        return getEnumMap(null);
    }

    public Map<String, Object> getEnumMap(Long id){
        // init
        this.fmUtils = new FmUtils("");

        //gen enums
        // List<Map> optionDataList = fmUtils.queryOptionDataListBySearch(templateService, id);

        //freemarker 参数
        Map<String, Object> map = new HashMap<>();
        //enum
        map.put("tmptData", fmUtils.queryTmptDataById(templateService, id));

        return map;
    }


    public Result getMap4TmptFile4UpdateTmptTable(String version, GenerateDto generateDto) {
        Result result = new Result();
        String tableName = this.templateTable.getTableName();
        // update version first
        A1templatetable templateTableNew = a1templatetableMapper.selectById(templateTable.getId());
        JSONObject jo = new JSONObject();
        jo.put("module", generateDto.getModuleName());
        jo.put("domain", generateDto.getDomainName());
        jo.put("packageDotParent", generateDto.getPackageDotParent());
        jo.put("stripPrefix", generateDto.getStripPrefix());
        jo.put("version", version);
        templateTableNew.setVersion(jo.toJSONString());
        a1templatetableMapper.updateById(templateTableNew);

        // return
        QueryWrapper columnQueryWrapper = new QueryWrapper();
        columnQueryWrapper.eq("deleteflag", 0);
        columnQueryWrapper.eq("tmpt_table_id", templateTable.getId());
        List<A1templatecolumn> columnList = a1templatecolumnMapper.selectList(columnQueryWrapper);
        templateTableNew.setTmptTbColumnList(columnList);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(templateTableNew);
        String tmptJson = JSONArray.toJSONString(jsonArray);
        // tmptJson = tmptJson.replaceAll("a1TemplateColumnDtoList", "tmptTbColumnList");

        result.getMap().put("tableName", tableName);
        result.getMap().put("tmptJson", tmptJson);

        return result.success();
    }

    public A1templatetable getTemplateTableNew() {
        A1templatetable templateTableNew = a1templatetableMapper.selectById(templateTable.getId());
        return templateTableNew;
    }



    public com.rt.easyjava.common.base.Result zipSourceFiles(Set<String> outFileFullPathList, HttpServletResponse response) throws IOException {
        File[] srcfile = new File[outFileFullPathList.size()];
        int i=0;
        for (String fileFullPath: outFileFullPathList) {
            File f = new File(fileFullPath);
            srcfile[i++] = f;
        }

        response.setContentType("APPLICATION/OCTET-STREAM");//返回格式为流
        // response.setContentType("application/zip");//返回格式为流
        response.setHeader("Content-Disposition", "attachment; filename=easycloud.zip");

        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        ZipUtils.zipFilesContainPath(srcfile, out);
        out.flush();
        out.close();

        return null;
    }


    public com.rt.easyjava.common.base.Result zipSourceFiles(String javaProjPath, String viewProjPath, String moduleName, String tableName, HttpServletResponse response) throws IOException {
        String projectNameJava = "java";
        String projectNameView = "vue";

        // path
        String mainPath = javaProjPath+ File.separator+moduleName+File.separator+"src"+File.separator+"main";
        String packagePath = "com"+File.separator+"rt"+File.separator+"easyjava"+File.separator+moduleName;
        String className = SwitchLineCamel.switch2Property(tableName).substring(0,1).toUpperCase()+SwitchLineCamel.switch2Property(tableName).substring(1);
        String classNameFirstWordLower = SwitchLineCamel.switch2Property(tableName).substring(0,1).toLowerCase()+SwitchLineCamel.switch2Property(tableName).substring(1);
        // path for zip
        String mainPath4zip = "easyjava"+File.separator+projectNameJava+File.separator+moduleName+File.separator+"src"+File.separator+"main";
        // zip文件
        //bean
        String beanPath = mainPath+File.separator+"java"+File.separator+packagePath+File.separator+"bean";
        String beanPath4zip = mainPath4zip+File.separator+"java"+File.separator+packagePath+File.separator+"bean";
        File beanFile = new File(beanPath+File.separator+className+"Biz.java");
        File beanBaseFile = new File(beanPath+File.separator+"base"+File.separator+className+".java");
        //dto
        String dtoPath = mainPath+File.separator+"java"+File.separator+packagePath+File.separator+"dto";
        String dtoPath4zip = mainPath4zip+File.separator+"java"+File.separator+packagePath+File.separator+"dto";
        File dtoFile = new File(dtoPath+File.separator+className+"BizDto.java");
        File dtoBaseFile = new File(dtoPath+File.separator+"base"+File.separator+className+"Dto.java");
        //mapper
        String mapperPath = mainPath+File.separator+"java"+File.separator+packagePath+File.separator+"mapper";
        String mapperPath4zip = mainPath4zip+File.separator+"java"+File.separator+packagePath+File.separator+"mapper";
        File mapperFile = new File(mapperPath+File.separator+className+"BizMapper.java");
        File mapperBaseFile = new File(mapperPath+File.separator+"base"+File.separator+className+"Mapper.java");
        //controller
        String controllerPath = mainPath+File.separator+"java"+File.separator+packagePath+File.separator+"controller";
        String controllerPath4zip = mainPath4zip+File.separator+"java"+File.separator+packagePath+File.separator+"controller";
        File controllerFile = new File(controllerPath+File.separator+className+"BizController.java");
        File controllerBaseFile = new File(controllerPath+File.separator+"base"+File.separator+className+"Controller.java");
        //service.impl
        String serviceImplPath = mainPath+File.separator+"java"+File.separator+packagePath+File.separator+"service"+File.separator+"impl";
        String serviceImplPath4zip = mainPath4zip+File.separator+"java"+File.separator+packagePath+File.separator+"service"+File.separator+"impl";
        File serviceImplFile = new File(serviceImplPath+File.separator+className+"BizServiceImpl.java");
        File serviceImplBaseFile = new File(serviceImplPath+File.separator+"base"+File.separator+className+"ServiceImpl.java");
        //service.intf
        String serviceIntfPath = mainPath+File.separator+"java"+File.separator+packagePath+File.separator+"service"+File.separator+"intf";
        String serviceIntfPath4zip = mainPath4zip+File.separator+"java"+File.separator+packagePath+File.separator+"service"+File.separator+"intf";
        File serviceIntfFile = new File(serviceIntfPath+File.separator+className+"BizService.java");
        File serviceIntfBaseFile = new File(serviceIntfPath+File.separator+"base"+File.separator+className+"Service.java");
        //mapperXml
        String mapperXmlPath = mainPath+File.separator+"resources"+File.separator+"mapper";
        String mapperXmlPath4zip = mainPath4zip+File.separator+"resources"+File.separator+"mapper";
        File mapperXmlFile = new File(mapperXmlPath+File.separator+className+"BizMapper.xml");
        File mapperXmlBaseFile = new File(mapperXmlPath+File.separator+"base"+File.separator+className+"Mapper.xml");
        //dozer
        String dozerPath = mainPath+File.separator+"resources"+File.separator+"dozer";
        String dozerPath4zip = mainPath4zip+File.separator+"resources"+File.separator+"dozer";
        File dozerFile = new File(dozerPath+File.separator+className+".xml");

        // views 文件夹
        String viewPath = viewProjPath+File.separator+"src"+File.separator+"views"+File.separator+"coding"+File.separator+moduleName+File.separator+classNameFirstWordLower;
        String viewPath4zip = "easyjava"+File.separator+projectNameView+File.separator+"src"+File.separator+"views"+File.separator+"coding"+File.separator+moduleName+File.separator+classNameFirstWordLower;
        File viewFilePath = new File(viewPath);

        // core tmpt file
        String tmptPath = javaProjPath+File.separator+/*"core"+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+*/"tmptFile"+File.separator+tableName;
        // String tmptPath4zip = "easyjava"+File.separator+projectNameJava+File.separator+"core"+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"tmptFile"+File.separator+tableName;
        String tmptPath4zip = "easyjava"+File.separator+projectNameJava+File.separator+/*"core"+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+*/"tmptFile"+File.separator+tableName;
        File tmptFilePath = new File(tmptPath);
        // 找最新的文件
        File[] listFiles = tmptFilePath.listFiles();
        File tmptFile = null;
        long modifiedTime = 0;
        for (File file : listFiles) {
            if (file.lastModified()>modifiedTime) {
                modifiedTime = file.lastModified();
                tmptFile = file;
            }
        }

        Map[] srcfileMap={
                getFileMap(beanPath4zip, beanFile),
                getFileMap(beanPath4zip+File.separator+"base", beanBaseFile),
                getFileMap(dtoPath4zip, dtoFile),
                getFileMap(dtoPath4zip+File.separator+"base", dtoBaseFile),
                getFileMap(mapperPath4zip, mapperFile),
                getFileMap(mapperPath4zip+File.separator+"base", mapperBaseFile),
                getFileMap(controllerPath4zip, controllerFile),
                getFileMap(controllerPath4zip+File.separator+"base", controllerBaseFile),
                getFileMap(serviceImplPath4zip, serviceImplFile),
                getFileMap(serviceImplPath4zip+File.separator+"base", serviceImplBaseFile),
                getFileMap(serviceIntfPath4zip, serviceIntfFile),
                getFileMap(serviceIntfPath4zip+File.separator+"base", serviceIntfBaseFile),
                getFileMap(mapperXmlPath4zip, mapperXmlFile),
                getFileMap(mapperXmlPath4zip+File.separator+"base", mapperXmlBaseFile),
                getFileMap(dozerPath4zip, dozerFile),
                getFileMap(viewPath4zip, viewFilePath), // views 文件夹
                getFileMap(tmptPath4zip, tmptFile), // tmpt file
        };
        //压缩后的文件
        File zipfile=new File(File.separator+"Users"+File.separator+"wxy2020"+File.separator+"workspace"+File.separator+"easyjava1.zip");
        ZipUtils.zipFilesContainPath(srcfileMap, zipfile);

        response.setContentType("APPLICATION/OCTET-STREAM");//返回格式为流
        // response.setContentType("application/zip");//返回格式为流
        response.setHeader("Content-Disposition", "attachment; filename=easyjava.zip");

        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        ZipUtils.zipFilesContainPath(srcfileMap, out);
        out.flush();
        out.close();

        return null;
    }

    private Map getFileMap(String path, File file) {
        Map map = new HashMap();
        map.put("path", path);
        map.put("file", file);
        return map;
    }

}
